package com.xfcycle.zebrabikegp.util;

import android.content.Context;
import android.content.Intent;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 用户状态改变
 */
public class UserStateChangeUtil {
	private HandleDataBySharePreferencesUtil handleDateBySharePreferences;
	private HandleDataByFileUtil handleDateByFile;
	private boolean loginState = false;
	private String cachePath;
	private Context context;
	
	//sp名称
	public final static String USER_STATE_CHANGE_SPNAME = "UserLoginInfo";
	
	//sp中键值对对应的keyֵ
	public final static String USER_STATE_KEY = "user_login_state";
	public final static String USER_ID_KEY = "user_id";
	public final static String USER_NAME_KEY = "user_name";
	public final static String USER_PASS_KEY = "user_password";
	public final static String USER_REAL_NAME_KEY = "user_realname";
	public final static String USER_PORTRAIT_PATH_KEY = "user_portrait_path";

	public UserStateChangeUtil(Context context) {
		this.context = context;
		this.handleDateBySharePreferences = HandleDataBySharePreferencesUtil.getInstance(context);
		this.handleDateByFile = new HandleDataByFileUtil();
		this.cachePath = context.getFilesDir().getParentFile().getPath() + "/cache";
	}

	/**
	 * 用户登录
	 * @param username
	 * @param userpass
	 * @return
	 */
	public boolean userLogin(String username, String userpass) {

		Map<String, String> data = new HashMap<String, String>();
		
		Intent intent = new Intent();
		intent.setAction("oldbook.userLogin.state");
		intent.putExtra("state", false);
		
		try {
			String request_path = "http://192.168.24.15:8080/eBuy_Web/login?username="
					+ URLEncoder.encode(username, "UTF-8") + "&userpass=" + URLEncoder.encode(userpass, "UTF-8");
			URL url = new URL(request_path);
			InputStream is = url.openStream();
			DataInputStream dis = new DataInputStream(is);

			data.put(USER_STATE_KEY, "false");

			if (1 == dis.readInt()) {
				data.put(USER_STATE_KEY, "true");
				
				Integer userid = dis.readInt();
				data.put(USER_ID_KEY, userid + "");
				intent.putExtra("userId", userid);
				
				data.put(USER_NAME_KEY, username);
				data.put(USER_PASS_KEY, userpass);
				data.put(USER_REAL_NAME_KEY, dis.readUTF());
				String portraitPath = cachePath + "/" + username + ".jpg";
				data.put(USER_PORTRAIT_PATH_KEY, portraitPath);

				byte[] portraitTemp = new byte[dis.readInt()];
				dis.read(portraitTemp);
				// 将图片写入到缓存中
				handleDateByFile.writePictureToCacheFile(portraitTemp, portraitPath);

				loginState = true;
				
				intent.putExtra("state", true);
				dis.close();
				is.close();
			}
			//将用户信息放到SharePreferences中
			handleDateBySharePreferences.putDateIntoSharePre(USER_STATE_CHANGE_SPNAME, data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		context.sendBroadcast(intent);
		return loginState;
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	public boolean exitUserLogin(){
		try {
			Set<String> keys = new HashSet<String>();
			
			keys.add(USER_STATE_KEY);
			keys.add(USER_ID_KEY);
			keys.add(USER_REAL_NAME_KEY);
			keys.add(USER_PORTRAIT_PATH_KEY);
			
			handleDateBySharePreferences.deleteTargetDataInSharePre(USER_STATE_CHANGE_SPNAME, keys);
			
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
